/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jeff.springWebTestNG.tx;

import com.jeff.springWebTestNG.SpringContextPovider;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;

/**
 *
 * @author winnie
 */
public class EventTransactionSynchronization extends TransactionSynchronizationAdapter {

    public EventTransactionSynchronization() {
    }

    @Override
    public void afterCommit() {
        //TODO some tasks needed on successful commit, such as sending message, email etc.

        //since resources locked for current transaction not yet released, 
        //must Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.
        System.out.println("Haha, afterCommit(), have u seen the commit completed successfully?");

        //TODO: flush audit queue
        SpringContextPovider.getApplicationContext().publishEvent(new TransactionAwareEvent(this));
        
        System.out.println("Audit Queue flushing event sent.");
    }

    @Override
    public void afterCompletion(int status) {
        if (status == STATUS_ROLLED_BACK) {
            System.out.println("Haha, afterRollback, have u seen the commit completed failure?");
        } else {
            System.out.println("Tx status = " + status);
        }

    }



}